/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nus.application.web.conference;

import com.nus.application.web.conference.form.ConferenceAuthorForm;
import com.nus.application.ejb.user.ConferenceEjbLocal;
import com.nus.application.ejb.user.UserEjbLocal;
import com.nus.application.web.base.BaseAction;
import com.nus.dataaccess.dao.review.ReviewAssignment;
import com.nus.dataaccess.dao.user.UserInfo;
import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.ValidationAware;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.inject.Inject;

/**
 *
 * @author robin.foe
 */
public class ConferenceAuthor extends BaseAction implements ValidationAware, Preparable{
    
    @Inject
    ConferenceEjbLocal conferenceEjb;
    
    @Inject
    UserEjbLocal userEjb;
    
    public String doList(){
        Map<String , Object> filters = new HashMap<String,Object>();
        filters.put("conferenceId", this.getForm().getConferenceAuthor().getConferenceId());
        this.form.setFullListSize(conferenceEjb.getTotalCountConferenceAuthor(filters));
        this.form.setPageNumber(super.getPage());
        this.getForm().setConferenceAuthors(
                conferenceEjb.paginateConferenceAuthor(form.calculateStartRow(super.getPage()), form.getObjectsPerPage(), null, false, filters)
                );
        return SUCCESS;
    }
    
    public String doMaintain(){
        com.nus.dataaccess.dao.conference.ConferenceAuthor conferenceAuthor = conferenceEjb.retrieveConferenceAuthor(this.getForm().getConferenceAuthor().getId());
        System.err.println(conferenceAuthor.getReviewer());
        this.getForm().setConferenceAuthor(conferenceAuthor);
        return SUCCESS;
    }
    
    public String doSave(){
        conferenceEjb.persistReviewAssignment(this.getForm().getConferenceAuthor());
        super.addActionMessage("Information successfully saved");
        return SUCCESS;
    }
    
    public String doAddReviewer(){
        ReviewAssignment reviewAssignment = new ReviewAssignment();
        reviewAssignment.setConferenceAuthorId(this.getForm().getConferenceAuthor().getId());
        this.getForm().getConferenceAuthor().getReviewer().add(reviewAssignment);
        return SUCCESS;
    }
    
    public String doRemoveReviewer(){
        this.getForm().getConferenceAuthor().getReviewer().remove(this.getForm().getSelectedIndex());
        return SUCCESS;
    }    
    
    private ConferenceAuthorForm form = new ConferenceAuthorForm();
    public ConferenceAuthorForm getForm() {return form;}
    public void setForm(ConferenceAuthorForm form) {this.form = form;}
    
    private List<UserInfo> reviewers = new ArrayList<UserInfo>();
    public List<UserInfo> getReviewers() {return reviewers;}
    public void setReviewers(List<UserInfo> reviewers) {this.reviewers = reviewers;}
    
    @Override
    public void prepare(){
        reviewers = userEjb.listAllUser();
    }
    
    @Override
    public void validate(){
        if(!super.isSaveEvent()){
            super.clearActionErrors();
            return;
        }
        
        List<Long> reviewerIds = new ArrayList<Long>();
        for(ReviewAssignment reviewAssignment : this.getForm().getConferenceAuthor().getReviewer()){
            if(reviewerIds.contains(reviewAssignment.getReviewerId())){
                super.addActionMessage("Reviewer Has been Assigned. Please select other Reviewer");
                break;
            }
            reviewerIds.add(reviewAssignment.getReviewerId());
        }
        
    }
    
}
